<template>
	<a-modal :title="title" :width="1400" :visible="visible" :confirmLoading="confirmLoading" :footer="null"
		@ok="handleSubmit" @cancel="handleCancel">
		<a-card :bordered="false">

			<div class="table-operator" v-if="hasPerm('emsOrder:handleProductionOrderProductComplete')">
				<a-button type="primary" :loading="suloading" @click="handleCompleteProduction">完成生产</a-button>
			</div>

			<s-table ref="table" size="default" :columns="columns" :data="loadData" :alert="true"
				:rowKey="(record) => record.code" :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
				:showPagination="false">

				<span slot="remark" slot-scope="text, record">
					<a-tooltip placement="topLeft" :title="record.material.remark">
						{{ record.material.remark }}
					</a-tooltip>
				</span>

				<template slot="thisCompleteNumber" slot-scope="text, record">
					<a-input-number v-model:value="record.thisCompleteNumber" :min="0" :step="1" style="width: 67px;" />
				</template>

				<span slot="actionInProduction" slot-scope="text, record">
					<a @click="$refs.productDetailForm.add(record)">产品明细</a>
				</span>
			</s-table>

			<product-detail-form ref="productDetailForm" @ok="handleOk" />

		</a-card>
	</a-modal>
</template>

<script>
import { STable } from '@/components'
import { page } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import { handleProductionOrderProductComplete } from '@/api/modular/main/emsorder/emsOrderManage'
import productDetailForm from '@/views/main/emsorder/emsOrderProductDetailForm'

export default {
	components: {
		STable,
		productDetailForm,
	},

	data() {
		return {
			suloading: false,
			title: null,
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {},  // 查询参数
			columns: [
				{
					title: '产品编号',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '产品名称',
					dataIndex: 'materialName'
				},
				{
					title: '数量',
					dataIndex: 'number'
				},
				{
					title: '已入库数量',
					dataIndex: 'inStoreNumber'
				},
				{
					title: '待入库数量',
					customRender: (text, record, index) => `${record.completedNumber - record.inStoreNumber}`
				},
				{
					title: '待生产数量',
					dataIndex: '',
					customRender: (text, record, index) => `${record.number - record.completedNumber}`
				},
				{
					title: '本次完成生产数量',
					scopedSlots: { customRender: 'thisCompleteNumber' }
				},
				{
					title: '序列号',
					dataIndex: 'extend1'
				},
				{
					title: '备注',
					dataIndex: 'remark',
					ellipsis: true,
					scopedSlots: { customRender: 'remark' }
				},
				{
					title: '操作',
					width: '150px',
					scopedSlots: { customRender: 'actionInProduction' }
				}
			],
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				this.queryParam.orderId = this.emsOrder.orderId
				this.queryParam.materialType = this.emsOrder.isOverseasStore == 'N' ? this.$businessEnum.MATERIAL_TYPE_ENUM.product.id : this.$businessEnum.MATERIAL_TYPE_ENUM.overseasProduct.id
				this.queryParam.pageSize = -1
				return page(Object.assign(parameter, this.queryParam)).then((res) => {
					res.data.rows.forEach(value => {
						value.thisCompleteNumber = 0
					})
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			visible: false,
			confirmLoading: false,
			emsOrder: null,
		}
	},
	methods: {
		show(record) {
			this.visible = true
			this.emsOrder = record;
			this.title = `产品列表（${record.orderNumber}）`
			setTimeout(() => {
				this.handleOk()
			}, 100)
		},
		toggleAdvanced() {
			this.advanced = !this.advanced
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		handleSubmit() {
		},
		handleCancel() {
			this.visible = false
			this.suloading = false
			this.$refs.table.clearSelected()
		},
		handleCompleteProduction() {

			let rows = this.selectedRows.filter(value => value.thisCompleteNumber > 0).map(value => {
				return {
					orderMaterialId: value.orderMaterialId,
					number: value.thisCompleteNumber
				}
			})
			if (rows.length == 0) {
				this.$message.error('错误：请选择产品并填写数量')
				return
			}
			this.suloading = true

			let parameter = {
				orderId: this.emsOrder.orderId,
				orderType: this.emsOrder.orderType,
				orderMaterials: rows
			}
			handleProductionOrderProductComplete(parameter).then(res => {
				if (res.code == 200) {
					this.$message.success('操作成功')
					this.$refs.table.clearSelected()
					this.$refs.table.refresh(true)
					this.$emit('ok')
					this.handleCancel()
					this.suloading = false
					this.$refs.table.clearSelected()
				} else {
					this.$message.error(res.message)
					this.suloading = false
				}
			}).finally(() => {
				setTimeout(() => {
					this.suloading = false
				}, 2000)
			})
		}
	}
}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
